<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_model extends CI_Model {

    private $_name = 'ci_user';

    var $validate = array(
        'category_edit' => array(
                    array(
                        'field'   => 'category_name',
                        'label'   => 'カテゴリ名',
                        'rules'   => 'required|xss_clean',
                    )
                ),
        );

    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->library(array('form_validation','MY_Validation','MY_FormParam'));
    }

    public function getCategoryList($params = array(), $isUsePager = false, $per_page = 0, $offset = 0, $order = 'category.lastup_datetime DESC'){
        if ($isUsePager) {
            $this->db->limit($per_page, $offset);
        }
        $where = '';
        if(!empty($params)){
            $where = $params;
        }
        $this->db->order_by($order);
        $where['disable'] = 0;
        $query = $this->db->get_where('category', $where);

        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $result[] = $rows;
            }
            return $result;
        }
    }

    public function getCategoryById($category_id){
        $query = $this->db->get_where('category',array('category_id' => $category_id),1);
        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $result = $rows;
            }
            return $result;
        }
        return false;
    }

    function exec_validation($valid_type) {
        $array    = $this->my_formparam->getFormParamList();
        $arrValidate = $this->validate[$valid_type];

        foreach($arrValidate as &$value){
            if($value['field'] == $array[$value['field']]['keyname']) {
                $value['label'] = $array[$value['field']]['disp_name'];
            }
        }

        $this->my_validation->set_rules($arrValidate);

        $status = $this->my_validation->run();

        $data['status'] = $status;
        $data['data'] = &$this->my_validation->_field_data;

        return $data;
    }
}
?>
